package net.common.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.modul.dict.service.DictSearchService;
import net.modul.dict.service.DictSortSearchService;
import net.modul.dict.vo.DictSortVo;
import net.modul.dict.vo.DictVo;

public class DictManager {

	private static Map<String, Map<String, DictVo>> DICT = new HashMap<String, Map<String, DictVo>>();
	private static Map<String, DictVo> DICTLIST = new HashMap<String, DictVo>();
	private static List<DictSortVo> DICTSORT = new ArrayList<DictSortVo>();
	
	/**
	 * 数据字典初始化
	 * 
	 */
	public static void init() {
		initDictSort();
		initDict();
	}
	
	/**
	 * 初始化字典分类
	 * 
	 */
	private static void initDictSort() {
		DictSortSearchService service = new DictSortSearchService();
		try {
			service.invoke();
			DICTSORT = service.getList();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 初始化字典数据
	 * 
	 */
	private static void initDict() {
		DictSearchService service = new DictSearchService();
		try {
			service.invoke();
			DICT = service.getList();
			Iterator<Map<String, DictVo>> iterator = DICT.values().iterator();
			while(iterator.hasNext()) {
				Map<String, DictVo> m = iterator.next();
				DICTLIST.putAll(m);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 字典分类
	 * 
	 * @return
	 */
	public static List<DictSortVo> getDictSort() {
		return DICTSORT;
	}
	
	/**
	 * 值
	 * 
	 * @param key
	 * @return
	 */
	public static String getDictByKey(String key) {
		DictVo vo = DICTLIST.get(key);
		if(vo != null) {
			return vo.getDict_name();
		} else {
			return null;
		}
	}
	
	/**
	 * 设置数据字典
	 * 
	 * @param key
	 * @param vo
	 */
	public static void setDict(String key, DictVo value) {
		Map<String, DictVo> map = DICT.get(key);
		map.put(value.getDict_num(), value);
		DICTLIST.put(value.getDict_num(), value);
	}
	
	/**
	 * 移除数据字典
	 * 
	 * @param key
	 * @param value
	 */
	public static void removeDict(String key, String value) {
		Map<String, DictVo> map = DICT.get(key);
		map.remove(value);
		DICTLIST.remove(value);
	}
	
	/**
	 * 字典值对象
	 * 
	 * @param key
	 * @return
	 */
	public static List<DictVo> getDict(String key) {
		return new ArrayList<DictVo>(DICT.get(key).values());
	}
	
	/**
	 * 字典对象
	 * 
	 * @param num
	 * @return
	 */
	public static DictVo getDictVo(String num) {
		return DICTLIST.get(num);
	}
}
